<template>
  <div>
    <div class="flex mar-t-15">
      <div>
        <el-checkbox v-model="checkAll" @change="handleCheckAll" label="全选" size="large" />
      </div>
      <div class="mar-l-20">
        <el-button type="text">导出</el-button>
      </div>
      <div class="mar-l-20">
        <!-- <el-button type="default" size="default">批量付款</el-button> -->
        <el-button type="default" size="default" @click = "batchCancel">批量取消</el-button>
      </div>
    </div>
    <div class="mar-t-15">
      <div class="list-header font-14 font-w flex">
        <span>商品</span>
        <span>颜色/尺码</span>
        <span>购买数</span>
        <span>总金额</span>
        <span>订单状态</span>
        <span>操作</span>
      </div>
      <div v-for="(item, index) of dataList" :key="index" class="mar-t-10">
        <div class="order-info flex flex-between pad-l-10 pad-r-20">
          <div class="v-align-c">
            <el-checkbox v-model="item.check" size="large" @change="handleCheck(item)" />
            <span class="mar-l-10 font-12">订单号: {{ item.orderNo }} </span>
            <span class="mar-l-10 font-12 font-color-9">{{ item.createTime }}</span>
          </div>
          <div class="v-align-c font-12 user-info">
            <span>收件人：{{ item.customerNickname }}</span>
            <span class="mar-l-15">手机号：{{ item.customerPhone }}</span>
          </div>
        </div>
        <div class="goods-info pad-t-13 pad-b-20 flex" v-for="(subItem, subIndex) of item.projectOrderGoodsList" :key="subIndex">
          <div class="flex pad-l-10">
            <img class="width-80 height-80" :src="subItem.goodsImageUrl" alt="">
            <div class="flex-col mar-l-10">
              <span class="font-12 font-color-3 line-1 goods-title">{{ subItem.goodsTitle}}</span>
              <span class="font-12 font-color-6 mar-t-4">货号：{{ subItem.goodsCode }}</span>
              <span class="font-12 font-color-6">单价：<span class="count">￥{{ subItem.goodsPrice }}</span></span>
              <!-- <span class="font-12 fast-tag flex flex-center mar-t-5">闪电下单</span> -->
            </div>
          </div>
          <div>
            <span class="font-12 font-color-6">{{ subItem.skuDetail }}</span>
          </div>
          <div>
            <span class="font-12 font-color-6">{{ subItem.goodsNum }}</span>
          </div>
          <div class="flex-col">
            <span class="font-14 font-color-3">￥{{ (parseFloat(subItem.goodsPayAmount) + parseFloat(subItem.goodsFreight)).toFixed(2) }}</span>
            <span class="font-12 font-color-6 mar-t-4">含运费{{ subItem.goodsFreight }}</span>
          </div>
          <div class="flex-col">
            <span class="font-12 font-color-6">{{dict.orderStatus[item.orderStatus]}}</span>
            <router-link :to="{name: 'my-order-detail', query: {orderId: item.orderId}}">
              <span class="font-12 font-color-3 mar-t-6 cursor-p">订单详情</span>
            </router-link>
          </div>
          <div class="flex-col font-12">
            <el-button type="primary" size="mini" @click="payOrder(item.orderId)">立即付款</el-button>
            <el-button type="text" size="default" @click="handleDelete(item.orderId)">删除订单</el-button>
          </div>
        </div>
      </div>
      <!-- 空数据展示 -->
      <div>
        <none-data v-if="dataList.length <= 0" />
      </div>
    </div>
    <div class="flex statistics-box" v-if="dataList.length > 0">
      <!-- <span class="mar-r-27">已选择订单<span class="font-w">{{ checkNum }}</span>个</span>
      <span>合计订单: <span class="font-w">￥{{ totalPrice.toFixed(2) }}</span></span> -->
    </div>
    <!-- <div class="flex flex-center mar-t-30">
      <page-number />
    </div> -->
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import noneData from '@/components/noneData/index.vue'
import { getDictByGroupName } from "@/api/common/common"
import { ElMessageBox, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
export default defineComponent({
  name: '',
  components: {
    noneData
  },
  props: {
    dataList: Object
  },
  setup(props,content) {
    const checkAll = ref(false)
    const checkNum = ref(0)
    const totalPrice = ref(0.00)
    const dict = ref({
      orderStatus: {}
    })
    onMounted(() => {
      initDict()
    })

    const initDict = () => {
      // 获取数据字典
      getDictByGroupName("ProjectOrderDTO").then((response) => {
        Object.assign(dict.value, response.data)
      })
    }
    const router = useRouter()
    const payOrder = (orderId:string) => {
      router.push({
        path: '/buy/pay-type',
        query: {
          orderId: orderId
        }
      })
    }
    // 批量取消订单
    const batchCancel = () => {
      if(checkNum.value <= 0) {
         ElMessage({
         type: 'warning',
         message: '请选择要取消的订单',
         })
      }
      ElMessageBox.confirm(
        '确认取消订单吗?',
        'Warning',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        props.dataList.forEach((item:object) => {
        if (item.check) {
          const params = {
          orderId: item.orderId,
          isDelete: false
        }
        content.emit("batchCancelOrder",params)
        }
      })
      }).catch(() => {})
    }

    // 删除
    const handleDelete = (orderId:string) => {
      ElMessageBox.confirm(
        '确认删除订单吗?',
        'Warning',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        const params = {
          orderId: orderId,
          isDelete: true
        }
        content.emit("handleSearch",params)
      }).catch(() => {})
    }

    const handleCheck = (item:object) => {
      let orderTotalPrice = parseFloat(item.orderPrice)
      if(item.check){
        checkNum.value = checkNum.value + 1
        totalPrice.value = totalPrice.value + orderTotalPrice
      } else {
        checkNum.value = checkNum.value - 1
        totalPrice.value = totalPrice.value - orderTotalPrice
      }
      // 全选是否显示
      if(checkNum.value === props.dataList.length) {
        checkAll.value = true
      }else{
        checkAll.value = false
      }
    }
    // 全选
    const handleCheckAll = (val:boolean) => {
      let total = 0
      if(props.dataList && props.dataList.length > 0){
        const records = props.dataList
        records.forEach((item:Object) => {
          item.check = val
          total += parseFloat(item.orderPrice)
        })
      }
      if (val) {
        checkNum.value = props.dataList.length
        totalPrice.value = total
      }else{
        checkNum.value = 0
        totalPrice.value = 0
      }
    }

    return {
      handleCheck,
      checkAll,
      checkNum,
      handleCheckAll,
      totalPrice,
      dict,
      handleDelete,
      batchCancel,
      payOrder
    }
  }
})
</script>

<style lang="scss" scoped>
  $darkBlue: #182245;
  $fontcolor: #323233;
  $td1: 371px;
  $td2: 149px;
  $td3: 228px;
  $td4: 105px;
  $td5: 144px;
  .list-header {
    width: 1140px;
    height: 48px;
    background: #F9F9F9;
    border: 1px solid #F5F5F5;
    color: $fontcolor;
    &>span {
      display: flex;
      align-items: center;
    }
    &>span:nth-child(1) {
      padding-left: 50px;
      width: $td1;
    }
    &>span:nth-child(2) {
      width: $td2;
    }
    &>span:nth-child(3) {
      width: $td3;
    }
    &>span:nth-child(4) {
      width: $td4;
    }
    &>span:nth-child(5) {
      width: $td5;
    }
  }
  .order-info {
    width: 1140px;
    height: 34px;
    background: #F5F7FB;
    .address-tag {
      background: $darkBlue;
      color: #fff;
      padding: 1px 5px;
    }
  }
  .user-info {
    color: $fontcolor;
  }
  .goods-info {
    &>div:not(:first-child) {
      margin-top: 7px;
    }
    &>div:nth-child(1) {
      width: $td1;
    }
    &>div:nth-child(2) {
      width: $td2;
    }
    &>div:nth-child(3) {
      width: $td3;
    }
    &>div:nth-child(4) {
      width: $td4;
    }
    &>div:nth-child(5) {
      width: $td5;
    }
    .goods-title {
      width: 189px
    }
    .count {
      color: #FF452C;
    }
    .fast-tag {
      width: 52px;
      height: 16px;
      border: 1px solid #FF7300;
      color: #FF7300;
    }
    .btn-color {
      color:#409EFF;
    }
  }
  .statistics-box {
    width: 1140px;
    height: 48px;
    background: #F9F9F9;
    border: 1px solid #F5F5F5;
    justify-content: flex-end;
    align-items: center;
    padding-left: 10px;
    padding-right: 20px;
  }
</style>

